/**
 * @description 进行数组的升序排列
 * @param {Array} b 进行操作的数组
 * @param {Boolean} n 是否改变原数组(默认改变原数组)
 * @return {Array} 操作后的数组
 */
export const sortAscend = function (b, n) {
  let a = [];
  n ? (a = JSON.parse(JSON.stringify(b))) : (a = b);
  let len = a.length;
  for (let i = 0; i < len - 1; i++) {
    for (let j = 0; j < len - 1 - i; j++) {
      if (a[j] > a[j + 1]) {
        let temp = a[j];
        a[j] = a[j + 1];
        a[j + 1] = temp;
      }
    }
  }
  return a;
};

// ---------------------------------------------------------------------
/**
 * @description 进行数组的降序排列
 * @param {Array} b 操作的数组
 * @param {Boolean} n 是否改变原数组(默认改变)
 * @return {Array} 操作后的数组
 */
export const gradeDown = function (b, n) {
  let a = [];
  n ? (a = JSON.parse(JSON.stringify(b))) : (a = b);
  let len = a.length;
  for (let i = 0; i < len - 1; i++) {
    for (let j = 0; j < len - 1 - i; j++) {
      if (a[j] < a[j + 1]) {
        let temp = a[j];
        a[j] = a[j + 1];
        a[j + 1] = temp;
      }
    }
  }
  return a;
};

// ---------------------------------------------------------------------
(()=>{
  let  arr = [1,2,5,12,15,18,22,33,55,66,89];
  dichotomy(arr,33);// 7
  // 二分法查找对应值
  function dichotomy(arr,val) {
      console.log(arr,val,'1',arr.length%2,arr.length/2);
      // 判断取左取右
      let type = '';
      // 判奇偶
      if(Number.isInteger(arr.length/2)){
          // 取中值
          let mid1 = arr[arr.length/2-1];
          let mid2 = arr[arr.length/2];
          // 判大小
          if(val!==mid1&&val!==mid2){
              type = val<mid1?'l':'r';
          }else if(val === mid1&&val!==mid2){
              return 
          }else if(val !== mid1&&val===mid2){
              return 
          }
      }else{
          // 取中值
          let mid1 = arr[Math.ceil(arr.length/2)];
          // 判大小
          if(mid1!==val){
              type = val<mid1?'l':'r';
          }else{
              return 
          }


      }
      
  }
  // 去空值
  function filterEmpty(arr) {
      return arr.filter(item=>item !== null&&item !== undefined);
  }

})()